package com.jwdf.uda.ui.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.ItemInvitationBinding;
import com.jwdf.uda.event.InvitationEvent;
import com.jwdf.uda.ui.activity.UserInfoActivity;
import com.jwdf.uda.ui.adapter.item.UserSimpleItem;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/16.
 */
public class InvitationAdapter extends AbsAdapter<UserSimpleItem> {
    private boolean mIsCurrentUser = false;
    private List<UserSimpleItem> mItems = new ArrayList<>();

    public InvitationAdapter(Context context) {
        super(context);
    }



    public void setCurrentUser(boolean isCurrentUser) {
        this.mIsCurrentUser = isCurrentUser;
    }

    public void setDetailData(PbBean.PbUserSimple userSimple) {
        mItems.clear();


    }

    @Override
    protected AbsViewHolder onCreateViewHolder(ViewGroup parent, int itemType) {
        return new UserSimpleViewHolder(mInflater.inflate(R.layout.item_invitation, parent, false));
    }

    private class UserSimpleViewHolder extends AbsViewHolder<UserSimpleItem> {
        private ItemInvitationBinding mBinding;

        public UserSimpleViewHolder(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
        }

        @Override
        protected void onBindView(final UserSimpleItem item) {
            final PbBean.PbUserSimple userSimple = item.userSimple;

            boolean isOwner = TextUtils.equals(userSimple.getUserId(), ConfigUtils.getUserId(mBinding.getRoot().getContext()));
            boolean isInvite=item.isInvite;
            if(isInvite){
                mBinding.ivInvitation.setImageResource(R.mipmap.ic_has_invited);
                mBinding.ivInvitation.setEnabled(false);
            }else {
                mBinding.ivInvitation.setImageResource(R.mipmap.ic_invitation);
                mBinding.ivInvitation.setEnabled(true);
            }
            mBinding.setIsOwner(isOwner);
            mBinding.setIsDesigner(userSimple.getIsDesigner());
            ImageLoader.getInstance().displayImage(userSimple.getAvatar().getUrl(),mBinding.ivHeader);
            mBinding.tvNickName.setText(userSimple.getNickname());
            mBinding.tvIntroduction.setVisibility(TextUtils.isEmpty(StringUtils.filterNullAndTrim(userSimple.getIntroduction())) ? View.GONE:View.VISIBLE);
            mBinding.tvIntroduction.setText(userSimple.getIntroduction());
            mBinding.ivIsDesigner.setVisibility(userSimple.getIsDesigner() ? View.VISIBLE : View.GONE);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UserInfoActivity.start(mBinding.getRoot().getContext(), userSimple.getUserId(), userSimple.getIsDesigner());
                }
            });
            if(!isInvite){
                mBinding.ivInvitation.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        mBinding.ivInvitation.setImageResource(R.mipmap.ic_has_invited);
                        EventBusUtil.post(new InvitationEvent(InvitationEvent.INVITATION_ANSWER, userSimple));
//                        mBinding.ivInvitation.setEnabled(false);
                    }
                });
            }

        }
    }
}
